#include	"WindowWarning.hpp"
#include	"ParseCsvWarning.hpp"

WindowWarning::WindowWarning(unsigned int heigthWinCode, unsigned int widthWinCode)
{
  (void)widthWinCode;
  this->_height = LINES - heigthWinCode;
  this->_width = COLS;
  this->_win = newwin(this->_height, this->_width, heigthWinCode, 0);
  box(this->_win, 0, 0);
  wrefresh(this->_win);
}

WindowWarning::~WindowWarning()
{
  
}

void	WindowWarning::checkEvent(int event)
{
  if (event == KEY_DOWN && this->_line < this->_listContain.size() - 1)
    this->_line += 1;
  else if (event == KEY_UP && this->_line > 0)
    this->_line -= 1;
  this->_scrollText->setListScrollText(getListWarningInLine(this->_line));
}

std::list<std::string>	WindowWarning::getListWarningInLine(unsigned int line)
{
  std::list<std::string>		a;
  unsigned int				cline = 0;
  std::list<ObjectContain *>::iterator	it;

  for (it = this->_listContain.begin(); it != this->_listContain.end(); it++)
    {
      if (line == cline)
	return ((*it)->getList());
      cline++;
    }
  return (a);
}

void	WindowWarning::setContentWin(const char *name_file, unsigned int size)
{
  std::string	a("./norme/");
  
  this->_line = 0;

  a.append(name_file);
  a.append(".warn");
  //open / read File source code
  this->_fileTree = new ContentFile(a.c_str());
  
  ParseCsvWarning parse(this->_fileTree->getList(), size);
  this->_listContain = parse.getListContainWarning();
  
  //open file source code
  this->_scrollText = new ScrollText(getListWarningInLine(this->_line), this->_height, this->_width, this->_win);
}

void	WindowWarning::update(bool focus)
{
  wclear(this->_win);
  if (focus)
    enableColorPair1(this->_win);
  box(this->_win, 0, 0);
  if (focus)
    disableColorPair1(this->_win);
  this->_scrollText->update();
  wrefresh(this->_win);
}
